package alo.p6.work;

import org.junit.Test;
import alo.p6.BinaryTreeNode;
//打印所有根叶路径
public class PrintPaths {
    @Test
    public void test() {
        int[] A = {1, 2, 4, 5, 3, 6, 7};
        int[] B = {4, 2, 5, 1, 6, 3, 7};
        BuildBinaryTree buildBinaryTree = new BuildBinaryTree();
        BinaryTreeNode root = buildBinaryTree.buildBinaryTree(B, A, 0, 6);
        printPaths(root);
        //1 2 4
        //1 2 5
        //1 3 6
        //1 3 7
    }
    public void printPaths(BinaryTreeNode root){
        int[] path=new int[256];
        printPaths(root,path,0);
    }
    //打印数组（root，数组，路径的数组索引）
    void printPaths(BinaryTreeNode root,int[] path,int pathLen){
        if (root==null) return;
        path[pathLen]=root.getData();
        pathLen++;
        //到达叶子结点，开始打印
        if (root.getLeft()==null&&root.getRight()==null){
            printArray(path,pathLen);
        }else {
            //利用递归，一层层向下
            printPaths(root.getLeft(),path,pathLen);
            printPaths(root.getRight(),path,pathLen);
        }
    }
    //打印数组
    private void printArray(int[] path, int pathLen) {
        for (int i = 0; i < pathLen; i++) {
            System.out.print(path[i]+" ");
        }
        System.out.println();
    }
}
